package com.mt.vsphere.core.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SmsUtil {
//
//    public static void main(String[] args) {
//        SmsUtil.sendVerifyCode("18328023238","123888");
//    }

    // 产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    // 产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    static final String accessKeyId = "LTAIWjR3iYPo0Rp3";
    static final String accessKeySecret = "NzOReD16g5jRgxNwAoGHbjfJPzIhJz";

    public static void sendVerifyCode(String phone,String randomCode){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "纤羽科技");
        request.putQueryParameter("TemplateCode", "SMS_151773757");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+randomCode+"\",\"product\":\"ytx\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static String getRandomCode(){
        Random random = new Random();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            s.append(random.nextInt(10));
        }
        return s.toString();
    }
}
